<template>
  <a-drawer width="600" :title="title" :visible="open" @close="handleClose">
    <a-form-model ref="formRef" :model="form" :rules="rules" :labelCol="{span: 4}" :wrapperCol="{span: 20}">
      <a-form-model-item label="操作人ID" prop="userId">
        <a-input v-model="form.userId" placeholder="请输入操作人ID" allow-clear/>
      </a-form-model-item>
      <a-form-model-item label="操作人账号" prop="userName">
        <a-input v-model="form.userName" placeholder="请输入操作人账号" allow-clear/>
      </a-form-model-item>
      <a-form-model-item label="操作IP" prop="operationIp">
        <a-input v-model="form.operationIp" placeholder="请输入操作IP" allow-clear/>
      </a-form-model-item>
      <a-form-model-item label="操作标题" prop="title">
        <a-input v-model="form.title" placeholder="请输入操作标题" allow-clear/>
      </a-form-model-item>
      <a-form-model-item label="操作耗时(ms)" prop="usageTime">
        <a-input v-model="form.usageTime" placeholder="请输入操作耗时(ms)" allow-clear/>
      </a-form-model-item>
      <a-form-model-item label="请求URL" prop="requestUrl">
        <a-input v-model="form.requestUrl" placeholder="请输入请求URL" allow-clear/>
      </a-form-model-item>
      <a-form-model-item label="请求方法" prop="requestMethod">
        <a-input v-model="form.requestMethod" placeholder="请输入请求方法" allow-clear/>
      </a-form-model-item>
      <a-form-model-item label="请求参数" prop="requestParams">
        <a-input v-model="form.requestParams" placeholder="请输入请求参数" allow-clear/>
      </a-form-model-item>
      <a-form-model-item label="响应结果" prop="responseResult">
        <a-input v-model="form.responseResult" placeholder="请输入响应结果" allow-clear/>
      </a-form-model-item>
      <a-form-model-item label="操作时间" prop="operationTime">
        <a-input v-model="form.operationTime" placeholder="请输入操作时间" allow-clear/>
      </a-form-model-item>
      <div class="bottom-control">
        <a-space>
          <a-button type="dashed" @click="handleClose">取消</a-button>
        </a-space>
      </div>
    </a-form-model>
  </a-drawer>
</template>
<script>
import {getSysOperationLog, saveSysOperationLog, updateSysOperationLog} from '@/api/system/sysOperationLog'

export default {
  data() {
    return {
      submitLoading: false,
      title: '',
      open: false,
      // 表单
      form: {},
      rules: {
        userId: [{required: false, trigger: ['blur', 'change'], message: '请输入操作人ID'}],
        userName: [{required: false, trigger: ['blur', 'change'], message: '请输入操作人账号'}],
        operationIp: [{required: false, trigger: ['blur', 'change'], message: '请输入操作IP'}],
        title: [{required: false, trigger: ['blur', 'change'], message: '请输入操作标题'}],
        usageTime: [{required: false, trigger: ['blur', 'change'], message: '请输入操作耗时(ms)'}],
        requestUrl: [{required: false, trigger: ['blur', 'change'], message: '请输入请求URL'}],
        requestMethod: [{required: false, trigger: ['blur', 'change'], message: '请输入请求方法'}],
        requestParams: [{required: false, trigger: ['blur', 'change'], message: '请输入请求参数'}],
        responseResult: [{required: false, trigger: ['blur', 'change'], message: '请输入响应结果'}],
        operationTime: [{required: false, trigger: ['blur', 'change'], message: '请输入操作时间'}]
      }
    }
  },
  methods: {
    /** 点击关闭 */
    handleClose() {
      this.reset()
      this.open = false
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        userId: undefined,
        userName: undefined,
        operationIp: undefined,
        title: undefined,
        usageTime: undefined,
        requestUrl: undefined,
        requestMethod: undefined,
        requestParams: undefined,
        responseResult: undefined,
        operationTime: undefined
      }
    },
    /** 点击新增 */
    handleAdd() {
      this.reset()
      this.title = '添加操作日志'
      this.open = true
    },
    /** 点击修改 */
    handleUpdate(row) {
      this.reset()
      getSysOperationLog(row.id).then(res => {
        this.title = '操作日志详情'
        this.open = true
        this.$nextTick(() => {
          this.form = res.data
        })
      })
    },
    /** 点击保存 */
    handleSubmit() {
      this.$refs.formRef.validate(valid => {
        if (valid) {
          this.submitLoading = true
          if (this.form.id && this.form.id > 0) {
            updateSysOperationLog(this.form).then(res => {
              this.$message.success(res.msg)
              this.handleClose()
              this.$emit('ok')
            }).finally(() => {
              this.submitLoading = false
            })
          } else {
            saveSysOperationLog(this.form).then(res => {
              this.$message.success(res.msg)
              this.handleClose()
              this.$emit('ok')
            }).finally(() => {
              this.submitLoading = false
            })
          }
        }
      })
    }
  }
}
</script>
